/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yy.bugzillahelp.tool;

import java.io.File;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class DeleteFileTemp {

    private static final Logger log = Logger.getLogger(DeleteFileTemp.class.getName());
    private static String firstDirectory = "";

    public static void main(String args[]) {
        //delClientTemp("C:/ccrl");
        //String path = "D:\\Project\\121\\ccrl122\\build\\web";
        //delNetTemp(path);
        //delAllTemp("D:/Project/ccrl122/web", "C:/ccrl");
    }

    /**
     *
     * @param path 根目录路径
     * @desc 删除net和client端产生的临时文件
     */
    public static void delAllTemp(String netPath, String clientPath) {
        delNetTemp(netPath);
        delClientTemp(clientPath);
    }

    /**
     *
     * @param path 根目录路径
     * @desc 删除net端产生的临时文件
     */
    public static void delNetTemp(String path) {
        //String relativePath = getPath(path);
        //<editor-fold defaultstate="collapsed" desc="获取要删除文件夹路径">
        String uploadExcelPath = path + File.separator + "UploadExcel";
        String excelExportPath = path + File.separator + "ExcelExport";
        String reportDocPath = path + File.separator + "reportDoc";
        String tcbrPath = path + File.separator + "TCBR";
        String forecastExcelExportPath = path + File.separator + "ForecastExcelExport";
        String tempPath = path + File.separator + "temp";
        String excelExportForRollPath = path + File.separator + "ExcelExportForRoll";
        //</editor-fold>

        //<editor-fold defaultstate="collapsed" desc="需要保留的模版文件">
        String[] uploadExcelRetain = {""};
        String[] excelExportRetain = {"Benchmark.xls", "Characterization.xls",
            "Export Internal Template Metric.xls",
            "Export Internal Template.xls",
            "Export Multi Template Metric.xls",
            "Export Multi Template.xls", "Export Template 4.0.xls",
            "Export Template Metric.xls", "Export Template.xls",
            "Export TOFP Template.xls", "Export TOPR Template.xls",
            "Mapping Rule Template.xls", "RollupSummary-Imperial.xls",
            "RollupSummary-Metric.xls"};
        String[] reportDocRetain = {""};
        String[] tcbrRetain = {""};
        String[] forecastExcelExportRetain = {"Cond-GIIP.xls",
            "Cond-GIIP-Inj.xls", "Cond-URR.xls", "Cond-URR-Inj.xls",
            "Gas-GIIP.xls", "Gas-URR.xls", "Oil-STOIIP.xls", "Oil-URR.xls"};
        String[] tempRetain = {"temp.loc"};
        String[] excelExportForRollRetain = {"RollupSummary.xls"};
        //</editor-fold>

        //<editor-fold defaultstate="collapsed" desc="清除文件">
        log.info("clear folder uploadExcel");
        firstDirectory = uploadExcelPath;
        delFolder(uploadExcelPath, uploadExcelRetain);
        log.info("clear folder excelExport");
        firstDirectory = excelExportPath;
        delFolder(excelExportPath, excelExportRetain);
        log.info("clear folder reportDoc");
        firstDirectory = reportDocPath;
        delFolder(reportDocPath, reportDocRetain);
        log.info("clear folder tcbr");
        firstDirectory = tcbrPath;
        delFolder(tcbrPath, tcbrRetain);
        log.info("clear folder forecastExcelExport");
        firstDirectory = forecastExcelExportPath;
        delFolder(forecastExcelExportPath, forecastExcelExportRetain);
        log.info("clear folder temp");
        firstDirectory = tempPath;
        delFolder(tempPath, tempRetain);
        log.info("clear folder excelExportForRoll");
        firstDirectory = excelExportForRollPath;
        delFolder(excelExportForRollPath, excelExportForRollRetain);
        //</editor-fold>

    }

    /**
     *
     * @param path 根目录路径
     * @desc 删除client端产生的临时文件
     */
    public static void delClientTemp(String path) {
        String relativePath = path;
        //<editor-fold defaultstate="collapsed" desc="获取要删除文件夹路径">
        String uploadExcelPath = relativePath + File.separator + "UploadExcel";
        String excelExportPath = relativePath + File.separator + "ExcelExport";
        String figurePath = relativePath + File.separator + "Figure";
        String forecastExcelExportPath = relativePath + File.separator + "ForecastExcelExport";
        String templatePath = relativePath + File.separator + "Template";
        //</editor-fold>

        //<editor-fold defaultstate="collapsed" desc="需要保留的模版文件">
        String[] uploadExcelRetain = {""};
        String[] excelExportRetain = {""};
        String[] figureRetain = {""};
        String[] forecastExcelExportRetain = {""};
        String[] templateRetain = {"Benchmark.xls", "Characterization.xls",
            "Export Internal Template Metric.xls",
            "Export Internal Template.xls",
            "Export Multi Template Metric.xls",
            "Export Multi Template.xls", "Export Template 4.0.xls",
            "Export Template Metric.xls", "Export Template.xls",
            "Export TOFP Template.xls", "Export TOPR Template.xls",
            "Mapping Rule Template.xls", "RollupSummary-Imperial.xls",
            "RollupSummary-Metric.xls", "Cond-GIIP.xls",
            "Cond-GIIP-Inj.xls", "Cond-URR.xls", "Cond-URR-Inj.xls",
            "Gas-GIIP.xls", "Gas-URR.xls", "Oil-STOIIP.xls", "Oil-URR.xls"};
        //</editor-fold>

        //<editor-fold defaultstate="collapsed" desc="清除文件">
        log.info("clear folder uploadExcel");
        firstDirectory = uploadExcelPath;
        delFolder(uploadExcelPath, uploadExcelRetain);
        log.info("clear folder excelExport");
        firstDirectory = excelExportPath;
        delFolder(excelExportPath, excelExportRetain);
        log.info("clear figure excelExport");
        firstDirectory = figurePath;
        delFolder(figurePath, figureRetain);
        log.info("clear folder forecastExcelExport");
        firstDirectory = forecastExcelExportPath;
        delFolder(forecastExcelExportPath, forecastExcelExportRetain);
        log.info("clear folder Template");
        firstDirectory = templatePath;
        delFolder(templatePath, templateRetain);
        //</editor-fold>


    }

    /**
     *
     * @param folderPath Delete folder path
     * @param retainFileName The need to retain the name of the file
     */
    private static void delFolder(String folderPath, String[] retainFileName) {
        if (retainFileName == null) {
            retainFileName = new String[]{""};
        }
        delFile(folderPath, retainFileName);
        String filePath = folderPath;
        filePath = filePath.toString();
        File myFilePath = new File(filePath);
        if (!firstDirectory.equals(folderPath)) {
            myFilePath.delete();
        }
    }

    private static boolean delFile(String folderPath, String[] retainFileName) {
        boolean flag = false;
        File file = new File(folderPath);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        StringBuffer retainFileNameStr = new StringBuffer();
        for (String fileName : retainFileName) {
            retainFileNameStr.append((fileName + "|"));
        }
        String retainFileStr = retainFileNameStr.toString();
        //<editor-fold defaultstate="collapsed" desc="删除文件夹或文件">
        for (int i = 0; i < tempList.length; i++) {
            // keep the retain file
            if (retainFileStr.contains(tempList[i])) {
                continue;
            }
            if (folderPath.endsWith(File.separator)) {
                temp = new File(folderPath + tempList[i]);
            } else {
                temp = new File(folderPath + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delFile(folderPath + File.separator + tempList[i], retainFileName);
                delFolder(folderPath + File.separator + tempList[i], retainFileName);
                flag = true;
            }
        }
        //</editor-fold>
        return true;

    }
}
